﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class lichdien : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        int page = Convert.ToInt32(Request.QueryString["page"]);
        string a = Request.QueryString.ToString();
        int start = 0;
        int take = 5;
        int total = 0;
        page = page - 1 < 0 ? 0 : page - 1;
        start = take*page;

        if (id != 0)
        {
            int count=0;
            var chuongtrinh = LichDien.GetByID(id,ref count);
            //int kq=chuongtrinh.Cast<object>().Count();
            if (count==0)               
            {
                rptChuongTrinh.DataSource = LichDien.GetAll(start,take,ref total);
                rptChuongTrinh.DataBind();

                Message.Text = "Không tìm thấy lịch diễn";
                Message.Visible = true;
            } 
            else
            {
                rptChuongTrinh.DataSource = chuongtrinh;
                rptChuongTrinh.DataBind();

            }
        } 
        else
        {
            rptChuongTrinh.DataSource = LichDien.GetAll(start, take, ref total);
            rptChuongTrinh.DataBind();
        }


        int totalPage = total/take;
        int mod = total%take;
        totalPage += mod==0?0:1;
        page += 1;
        int startPage = page - 3 < 1 ? 1 : (page - 2 > totalPage - 2) ? totalPage - 2 : page - 2;
        int endPage = startPage == 1 ? 3 : (startPage + 2 >= totalPage) ? totalPage : startPage + 2;
        int pageToShow = 3;
        string pagination = "";
        pagination = "<div class=\"pagination pagination-right\">";
        pagination += "<ul>";
        if(totalPage > pageToShow)
        {
            pagination += "<li " + ((page <= pageToShow) ? "class=\"disabled\"" : "") + "><a href=\"lichdien.aspx\">&laquo;</a></li>";
            pagination += string.Format("<li {0}><a href=\"lichdien.aspx?page={1}\">&lt;</a></li>", ((page == 1) ? "class=\"active\"" : ""), (page > 1) ? page - 1 : 1);
            for (int i = startPage; i <= endPage; i++)
            {
                pagination += string.Format("<li><a href=\"lichdien.aspx?page={0}\">{0}</a></li>", i);
            }
            pagination += string.Format("<li {0}><a href=\"lichdien.aspx?page={1}\">&gt;</a></li>", ((page == totalPage) ? "class=\"active\"" : ""), (page < totalPage) ? page + 1 : totalPage);
            pagination += string.Format("<li {0}><a href=\"lichdien.aspx?page={1}\">&raquo;</a></li>", (page>=totalPage-pageToShow)?"class=\"disable\"":"", totalPage);
        } else
        {
            for (int i = 1; i <= totalPage; i++)
            {
                pagination += string.Format("<li {1}><a href=\"lichdien.aspx?page={0}\">{0}</a></li>", i, page==i?"class=\"active\"":"");
            }
        }
        pagination += "</ul>";
        pagination += "</div>";

        paging.Text = pagination;

    }

}